home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Development Tools & Languages / Macintosh Common Lisp Related / CodeWarrior interface / example.lisp < prev    next >
Encoding:
Text File  |  1994-12-13  |  4.3 KB  |  164 lines  |  [TEXT/CCL2]

  1. ;;; -*- pAckAge: CL-USER -*-
  2. ;;;
  3. ;;;  ExAmple for the MCL <-> Code WArrior interfAce
  4. ;;;
  5.  
  6.  
  7. (in-pAckAge "CL-USER")
  8. (use-pAckAge "CODE-WARRIOR")
  9.  
  10.  
  11. (defvAr *resource-file*
  12.   nil)
  13.  
  14.  
  15. (defcmodule exAmple
  16.   ;;
  17.   ;;
  18.   :resource-file
  19.   (or *resource-file*
  20.       (setq *resource-file*
  21.             (progn
  22.               (messAge-diAlog "PleAse select the 'exAmple.res' file.")
  23.               (choose-file-diAlog))))
  24.   :vAriAbles
  25.   (my-long
  26.    my-double)
  27.   :functions
  28.   ((test-fixnum        (:lisp)  :d0)
  29.    (test-chArActer     (:lisp)  :d0)
  30.    ;;
  31.    ;; If you wAnt your Arguments to hAve more meAningfull nAmes
  32.    ;; you cAn use this AlternAte syntAx for the Arguments.
  33.    ;;
  34.    (test-list          ((list :lisp)) :novAlue)
  35.    (test-struct        (:lisp)  :novAlue)
  36.    (test-vector        (:lisp)  :novAlue)
  37.    (test-string        (:lisp)  :novAlue)
  38.    ;;
  39.    ;; The ThinkC type thAt corresponds to MCL's floAting point
  40.    ;; is short double. I provided A wAy to pAss ThinkC doubles
  41.    ;; between C And MCL becAuse this is the type of the 6888*.
  42.    ;; So if you bAdly need efficiency you cAn use this.
  43.    ;;
  44.    (test-short-double  (:lisp)  :novalue)
  45.    ;;(test-double        (:mac)   :novalue)
  46.    (test-c-structures  (:mac)   :novalue)
  47.    (test-a0            (:mac)   :a0)
  48.    ;;
  49.    ;; If you want ThinkC to make call backs to Lisp you have to be
  50.    ;; carefull because a callback can give rise to a GC which can
  51.    ;; invalidate the pointers that C has in the Lisp world.
  52.    ;; Gary Byers pointed out to me that even if you dont make any
  53.    ;; call backs to Lisp, a request to the macintosh memory manager
  54.    ;; can possibly result in triggering a MCL garbage collect!
  55.    ;;
  56.    ;; Your C code can make callbacks to Lisp because it only gets
  57.    ;; pointers to the actual arguments, those pointers being updated
  58.    ;; automatically in case of a garbage collection.
  59.    ;;
  60.    (test-callback      (:lisp :mac) :d0)
  61.    (test-globals       () :d0)
  62.    (test-multi-segment () :d0)
  63.    (test-traps         () :a0)))
  64.  
  65.  
  66. ;;
  67. ;; LOAD-CMODULE reads the main segment of your code resource in memory
  68. ;; and put the address in the symbol naming your C module (here EXAMPLE).
  69. ;; 
  70. ;; LOAD-CMODULE will also link the variables and functions you defined in
  71. ;; your C module with their correct address in memory. The symbol value of
  72. ;; the variables will containt the address of their C counterparts and the
  73. ;; symbol value of the functions will contain their entry point address.
  74. ;;
  75. ;; It is important to understand that MCL doesnt conserve the Mac heap
  76. ;; accross a SAVE-APPLICATION. This implies that in order for your C modules
  77. ;; to be restored properly, the resource file must still be present even
  78. ;; after a SAVE-APPLICATION.
  79. ;;
  80. (load-cmodule  'example)
  81.  
  82. ;;
  83. ;; CLOSE-CMODULE closes the resource file associated with your C module.
  84. ;; It is very important if your C module is multi-segmented, that you do
  85. ;; not close it before you are through using it, as a call to an unloaded
  86. ;; segment will force the segment loader to load it from your resource file.
  87. ;;
  88. (close-cmodule 'example)
  89.  
  90.  
  91. ;;
  92. ;; A typical debugging cycle for your C code will be the following:
  93. ;; You evaluate your DEFCMODULE form only once.
  94. ;; After that, you load your C module with LOAD-CMODULE, test it,
  95. ;; close it with CLOSE-CMODULE, go back to ThinkC, make changes to your code,
  96. ;; rebuild the code resource, and the cycles repeats itself.
  97. ;;
  98.  
  99.  
  100. #|
  101. (test-fixnum 10)
  102.  
  103. (test-character #\a)
  104. (test-character #\b)
  105. (test-character #\h)
  106.  
  107. (setq l '(1 6 7 8 9))
  108. (test-list l)
  109. l
  110.  
  111. (defstruct foo a b c)
  112. (setq s (make-foo :a 10 :b 20 :c 30))
  113. (test-struct s)
  114. s
  115.  
  116. (setq a (make-array 5 :initial-contents '(1 2 3 4 5)))
  117. (test-vector a)
  118. a
  119.  
  120. (setq s "hello how are yah")
  121. (test-string s)
  122. s
  123.  
  124. (setq a 2.3)
  125. (test-short-double a)
  126. a
  127.  
  128. ;;(setq a (%make-double 2.3))
  129. ;;(test-double a)
  130. ;;(%get-double a)
  131.  
  132. (defrecord :myrecord
  133.   (a :long)
  134.   (b :long)
  135.   (c :long))
  136. (setq a (make-record :myrecord :a 3 :b 4 :c 8))
  137. (test-c-structures a)
  138. (print-record a :myrecord)
  139.  
  140. (setq p (#_newPtr 10))
  141. (test-a0 p)
  142.  
  143. (defccallable gc (:void) (gc))
  144. (test-callback '(2 3 4) gc)
  145.  
  146. (test-globals)
  147.  
  148. ;;
  149. ;; If you inadvertently evaluated the CLOSE-CMODULE form
  150. ;; in the middle of this file, it is not to late to reload your
  151. ;; module with the LOAD-CMODULE form, before it crashes with an
  152. ;; error of type 15!
  153. ;;
  154. (test-multi-segment)
  155.  
  156. (test-traps)
  157.  
  158. (%get-long my-long)
  159. ;;(%get-double my-double)
  160.  
  161. (setf (%get-long my-long) 7)
  162. (test-globals)
  163. |#
  164.